<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<div style="margin-top: 10px;">
	<div class="hot-container">
		<div id="hot_dict_column"></div>
	</div>
</div>
<script type="text/javascript">
	function createSpreadsheetData(rowCount, colCount) {
		rowCount = typeof rowCount === 'number' ? rowCount : 100;
		colCount = typeof colCount === 'number' ? colCount : 4;
		var rows = [], i, j;
		for (i = 0; i < rowCount; i++) {
			var row = [];
			for (j = 0; j < colCount; j++) {
				row.push('');
			}
			rows.push(row);
		}
		return rows;
	}
	
	var typeCategory=['varchar','tinyint','smallint','mediumint','int','integer','bigint','real','double','float','decimal','numeric','char','binary','varbinary','date','time','datetime','timestamp','year','tinyblob','blob','mediumblob','longblob','tinytext','text','mediumtext','longtext','enum','set','point','linestring','polygon','geometry','multipoint','multilinestring','multipolygon','geometrycollection'];
	$(function() {
		initTable();
		
		
		setTimeout(function(){
			$("#search").click();
		},500);
	});
	
	var $hot=null;
	var empty=function(value, callback){
		setTimeout(function(){
			callback(!$.isNull(value));	
		},10);
	};
	function initTable(){
		
		var hot_dict_column = document.getElementById('hot_dict_column');

		$hot = new Handsontable(hot_dict_column, {
			rowHeaders : true,
			autoColumnSize:true,
			stretchH:'last',
			contextMenu: ['row_above', 'row_below', 'remove_row'],
			colHeaders : [ '字段名'],
			colWidths:[200],
			columns: [{data:'propertyName',validator:empty},
			           ]
		});

		$hot.updateSettings({
			contextMenu : {
				items : {
					"row_above" : {name : '在上面添加行'},
					"row_below" : {name : '在下面添加行'},
					"remove_row" : {name : '删除当前行'}
				}
			}
		});
	}
	
	function loadTable(){
		var key1 = '${key1}';
		//alert("444444");
		//alert(key1+"55555");
		var excelId=key1;
		//alert("11111");
		//alert(excelId+"22222");
		var url=$.getVirtualPath()+"/admin/excel_head/dictColumnJson?excelId="+excelId;
		var json=$.getJsonPost(url);
		if($.isNull(json)){
			return getEmptyData();
		}
		return json;
	}
	
	
	
	function getEmptyData() {
		var hot_borders_data = createSpreadsheetData(10, 8);
		return hot_borders_data;
	}
	 
	function reload(){
		//alert("333333333");
		var data=loadTable();
		$hot.loadData(data);
		$hot.render();
	}
	
	function saveForm(){
		deleteEmpty();
		
		$hot.validateCells(function(result){
			if(!result){
				alert("表格有错误信息，检查表格红色区域");
				return;
			}
			var dictColumns=getDataJson();
			var url="${t_virtualpath}/admin/excel_head/save";
			$.ajax({
				url:url,
				type:'post',
				dataType:'json',
				contentType:"application/json",
				data:JSON.stringify(dictColumns),
				success:function(result){
					$.topNotify(result.message,result.resultType);	
				}
			});
			
		});
		
	}
	
	function getDataJson(){
		var rows=$hot.getSourceData();
		var columns=[];
		for(var i=0;i<rows.length;i++){
			var data=rows[i];
			columns.push({"propertyName":data.propertyName,
						 });
		}
		return columns;
	}
	
	//删除空行
	function deleteEmpty(){
		var rows=$hot.countRows();
		for(var i=rows-1;i>=0;i--){
			if($hot.isEmptyRow(i)){
				$hot.alter('remove_row', i);
			}
		}
	}
	
</script>

<%@ include
	file="/WEB-INF/views/shared/base/scripts/limitless/default/handsontable.jsp"%>